﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OpenPop.Pop3;
using OpenPop.Mime;
using System.Data;
public partial class mail : System.Web.UI.UserControl
{
    
    mypopclient pop3Client = new mypopclient();
    public string Msgid
    {

        get { return msgid.Value; }

        set { msgid.Value = value; }

    }
    public string From
    {

        get { return from.Text; }

        set { from.Text = value; }

    }
    public string Date
    {

        get { return date.Text; }

        set { date.Text = value; }

    }
    public string Address
    {

        get { return address.Text; }

        set { address.Text = value; }

    }
    public string Body
    {

        get { return body.Text; }

        set { body.Text = value; }

    }
    public string Importance
    {

        get { return importance.Text; }

        set { importance.Text = value; }

    }
  

    protected void Page_Load(object sender, EventArgs e)
    {

        pop3Client.Connect();
        OpenPop.Mime.Message message = pop3Client.GetMessage(int.Parse(Msgid));
        if (!Page.IsPostBack)
        {
                this.Body = HttpUtility.HtmlEncode(message.FindFirstPlainTextVersion().GetBodyAsText());
                this.From = message.Headers.From.ToString();
                this.Address = message.Headers.Subject;
                this.Date = message.Headers.DateSent.ToString();
                this.Importance = message.Headers.Importance.ToString();
        }
        bool ant = bool.Parse(((DataRow)Session["allsettings"])["noattach"].ToString());

        if (!ant)
        {
        if (message.FindAllAttachments().Count > 0)
        {
            int i = 0;
            foreach (MessagePart at in message.FindAllAttachments())
            {
                LinkButton hl = new LinkButton();

                hl.ToolTip = i.ToString();
                hl.EnableViewState = true;
                hl.Text = at.FileName;
                attachments.Controls.Add(hl);
                LiteralControl lt=new LiteralControl(" <br />");
                attachments.Controls.Add(lt);
                hl.Click += new EventHandler(this.download_Click);
                i++;
            }
            attachments.Visible = true;
        }
        }

    }
  
    protected void reply_Click(object sender, EventArgs e)
    {
        Editor1.Visible = true;
        body.Visible = false;
        Editor1.Content = "<br/>--------------------<br/>" + body.Text;
        sorc.Visible = true;
  
       
        
    }
    protected void forward_Click(object sender, EventArgs e)
    {
        Editor1.Visible = true;
        body.Visible = false;
        Editor1.Content = "<br/>--------------------<br/>" + body.Text;
        fors.Visible = true;
        sorc.Visible = true;
      
        subject.Text = "Fwd:" + address.Text;
    }
   
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Mails.aspx");
    }
    public void download_Click(object sender, EventArgs e)
    {
        pop3Client.Connect();
        int index = int.Parse(((LinkButton)sender).ToolTip);
        MessagePart part = pop3Client.GetMessage(int.Parse(msgid.Value)).FindAllAttachments()[index];
        Response.ContentType = part.ContentType.ToString();
        Response.AddHeader("content-disposition", "attachment; filename=" + part.FileName);
        Response.BinaryWrite(part.Body);
        Response.Flush();


    }
    public void sendwithattach(string too, string subjects)

    {
        EO.Web.AJAXPostedFile[] files = AJAXUploader1.PostedFiles;
        string[] names = new string[files.Length];
        for (int i = 0; i < files.Length; i++)
        {


            names[i] = files[i].TempFileName + "," + files[i].FullClientFileName;//System.IO.Path.GetFileName(files[i].TempFileName); ////Server.MapPath("~/temp/Example.xml") + 
            // Server.MapPath("~\temp\\") + files[i].TempFileName;

        }
        DB.sendingmail(too, subjects, Editor1.Content, names);
        AJAXUploader1.ClearPostedFiles();
    
    
    }



    protected void send_Click(object sender, EventArgs e)
    {
        
        if (fors.Visible)
        {
            if (To.Text.Contains(','))
            {
                string[] mails = To.Text.Split(',');
                foreach (string dd in mails)
                {
                    if (AJAXUploader1.PostedFiles.Length>0)
                    {
                      //  DB.sendingmail(dd, subject.Text, Editor1.Content, AsyncFileUpload1.FileContent, AsyncFileUpload1.FileName);
                        sendwithattach(dd, subject.Text);

                    }
                    else
                    {
                        DB.sendingmail(dd, subject.Text, Editor1.Content); //TextBox1.Text);
                    }
                   
                }
            }
            else
            {
                if (AJAXUploader1.PostedFiles.Length>0)
                {
                    //DB.sendingmail(To.Text, subject.Text, Editor1.Content, AsyncFileUpload1.FileContent, AsyncFileUpload1.FileName);
                    sendwithattach(To.Text, subject.Text);

                }
                else
                {
                    DB.sendingmail(To.Text, subject.Text, Editor1.Content);
                }
               

            }
           // too = To.Text;

        }
        else
        {
            if (AJAXUploader1.PostedFiles.Length>0)
            {

               // DB.sendingmail(from.Text, "Re:" + address.Text, Editor1.Content, AsyncFileUpload1.FileContent, AsyncFileUpload1.FileName);
                sendwithattach(from.Text, "Re:" + address.Text);
            }
            else
            {
                DB.sendingmail(from.Text, "Re:" + address.Text, Editor1.Content);
            }
           
        
        }
        Page.ClientScript.RegisterStartupScript(this.GetType(), "reports", " Sents();", true);
        
        

    }
    protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
    {


        HttpFileCollection postedfile = Request.Files;
        if (postedfile[0].ContentLength > 5196000)
        {

            return;

        }


    }

    protected void new_Click(object sender, EventArgs e)
    {
        attachments.Visible = false;
        Editor1.Visible = true;
        body.Visible = false;
        //fors.Visible = true;
        sorc.Visible = true;
        To.Text = "";
        subject.Text = "";
        Editor1.Content = ""  ;
       // Editor1.Content.Remove(0);
      //  Page.ClientScript.RegisterStartupScript(this.GetType(), "reports", " cleareditorcontent();", true);

    }
   
}
